package com.youka.bussiness.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youka.bussiness.common.utils.BaseUtil;
import com.youka.bussiness.dao.entity.Goods;
import com.youka.bussiness.service.GoodsService;
import com.youka.bussiness.service.req.GoodsReqDto;
import com.youka.bussiness.service.req.GoodsUpdateDto;
import com.youka.bussiness.service.req.GoodsPageDto;
import com.youka.bussiness.service.res.GoodsDto;;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youka.bussiness.common.bean.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.youka.bussiness.dao.GoodsMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品 Service 实现类
 *
 * @author lfc
 * @date 2024-06-30 16:47:29
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    /**
     * 查询详情
     *
     * @param id 查询参数
     * @return goodsDto
     */
    @Override
    public GoodsDto selectById(long id) {
        GoodsDto goodsDto = new GoodsDto();
        Goods goods = goodsMapper.selectById(id);
        if (goods != null){
            BeanUtils.copyProperties(goods,goodsDto);
        }
        return goodsDto;
    }

    /**
     * 集合查询商品
     *
     * @param dto 查询参数
     * @return Page
     */
    @Override
    public List<GoodsDto> selectByList(GoodsReqDto dto) {
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper();
        List<Goods> Goodss = goodsMapper.selectList(wrapper);
        return BaseUtil.listToList(Goodss, GoodsDto.class);
    }

    /**
     * 分页查询商品
     *
     * @param dto 查询参数
     * @return Page
     */
    @Override
    public IPage selectByPage(GoodsPageDto dto) {
        QueryWrapper<Goods> wrapper = Page.getPageWrapper(dto);
        return Page.copyProperties(this.page(Page.defaultPage(dto.getPageIndex(), dto.getPageSize()), wrapper)
                ,GoodsDto.class);
    }

    /**
     * 新增商品
     *
     * @param dto 商品
     * @return 新增结果
     */
    @Override
    public void insert(GoodsReqDto dto) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(dto,goods);
        goods.setCreTime(LocalDateTime.now());
        goodsMapper.insert(goods);
    }

    /**
       * 统计
        * @param dto
       * @return
       */
    @Override
     public  GoodsDto selectCount(GoodsReqDto dto){
        GoodsDto resp = new GoodsDto();
        goodsMapper.selectCount(new LambdaQueryWrapper<Goods>());
        return resp;
    }

    /**
     * 更新商品
     * (根据主键)
     *
     * @param dto 商品
     * @return 更新结果
     */
    @Override
    public void update(GoodsUpdateDto dto) {
        Goods goods =new Goods();
        BeanUtils.copyProperties(dto,goods);
        goods.setUpdTime(LocalDateTime.now());
        goodsMapper.updateById(goods);
    }

    /**
     * 删除商品
     * (根据主键)
     *
     * @param id id
     * @return 删除结果
     */
    @Override
    public void deleteById(Integer id) {
        goodsMapper.deleteById(id);
    }


}
